package com.hboxs.asl.entity;

import com.fasterxml.jackson.annotation.JsonValue;
import com.hboxs.asl.util.DateUtils;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Similarity;
import org.wltea.analyzer.lucene.IKSimilarity;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Entity - 课程
 */
@Indexed
@Similarity(impl = IKSimilarity.class)
@Entity
@Table(name = "xx_course_trade")
@SequenceGenerator(name = "sequenceGenerator", sequenceName = "xx_course_trade_sequence")
public class CourseTrade extends BaseEntity {

    private static final long serialVersionUID = 1475770998780685482L;

    /**
     * 支付状态
     */
    public enum PaymentStatus {

        /**
         * 未支付
         */
        unpaid,

        /**
         * 已支付
         */
        paid;

        private static Map<String, CourseTrade.PaymentStatus> map = new HashMap<>();

        static {
            map.put("未支付", unpaid);
            map.put("已支付", paid);
        }

        public static CourseTrade.PaymentStatus forValue(String value) {
            return map.get(value);
        }

        @JsonValue
        public String toValue() {
            for (Map.Entry<String, CourseTrade.PaymentStatus> entry : map.entrySet()) {
                if (entry.getValue() == this) {
                    return entry.getKey();
                }
            }

            return "";
        }

        public static CourseTrade.PaymentStatus getDefault() {
            return CourseTrade.PaymentStatus.unpaid;
        }
    }

    /**
     * 支付状态
     */
    private PaymentStatus paymentStatus;

    /**
     * 支付方式名称
     */
    private String paymentMethodName;

    /**
     * 支付方式
     */
    private com.hboxs.asl.entity.PaymentMethod paymentMethod;

    /**
     * 订单编号
     */
    private String sn;

    /**
     * 联系人
     */
    private String linkman;

    /**
     * 手机号
     */
    private String phone;

    /**
     * 备注
     */
    private String note;

    /**
     * 购买用户
     */
    private Member buyer;

    /**
     * 是否已评论
     */
    private Boolean hasReview;

    //==============================================冗余字段=============================================================

    public enum CourseType {

        /**
         * 未直播
         */
        nolive,

        /**
         * 直播中
         */
        live,

        /**
         * 录播
         */
        finished;

        private static Map<String, CourseTrade.CourseType> map = new HashMap<>();

        static {
            map.put("未直播", nolive);
            map.put("直播中", live);
            map.put("录播", finished);
        }

        public static CourseTrade.CourseType forValue(String value) {
            return map.get(value);
        }

        @JsonValue
        public String toValue() {
            for (Map.Entry<String, CourseTrade.CourseType> entry : map.entrySet()) {
                if (entry.getValue() == this) {
                    return entry.getKey();
                }
            }

            return "";
        }

        public static CourseTrade.CourseType getDefault() {
            return CourseTrade.CourseType.nolive;
        }
    }

    /**
     * 课程状态
     */
    public CourseType courseType;

    /**
     * 课程id
     */
    private Long courseId;

    /**
     * 标题
     */
    private String title;

    /**
     * 封面
     */
    private String imageURL;

    /**
     * 简介
     */
    private String introduction;

    /**
     * 是否收费
     */
    private Boolean charge;

    /**
     * 是否加密
     */
    private Boolean encrypt;

    /**
     * 设置分享密码

     */
    private String password;

    /**
     * 直播日期
     */
    private Date pubdate;

    /**
     * 价格
     */
    private BigDecimal price;

    /**
     * 老师id
     */
    private Long teacherId;

    /**
     * 老师名称
     */
    private String teacherName;

    /**
     * 老师头像
     */
    private String teacherAvator;

    /**
     * 分类id
     */
    private Long categoryId;

    /**
     * 分类名称
     */
    private String categoryName;

    public CourseType getCourseType() {
        return courseType;
    }

    public void setCourseType(CourseType courseType) {
        this.courseType = courseType;
    }

    public PaymentStatus getPaymentStatus() {
        return paymentStatus;
    }

    public void setPaymentStatus(PaymentStatus paymentStatus) {
        this.paymentStatus = paymentStatus;
    }

    public String getSn() {
        return sn;
    }

    public void setSn(String sn) {
        this.sn = sn;
    }

    public String getLinkman() {
        return linkman;
    }

    public void setLinkman(String linkman) {
        this.linkman = linkman;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    public Member getBuyer() {
        return buyer;
    }

    public void setBuyer(Member buyer) {
        this.buyer = buyer;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getImageURL() {
        return imageURL;
    }

    public void setImageURL(String imageURL) {
        this.imageURL = imageURL;
    }

    public String getIntroduction() {
        return introduction;
    }

    public void setIntroduction(String introduction) {
        this.introduction = introduction;
    }

    public Boolean getCharge() {
        return charge;
    }

    public void setCharge(Boolean charge) {
        this.charge = charge;
    }

    public Boolean getEncrypt() {
        return encrypt;
    }

    public void setEncrypt(Boolean encrypt) {
        this.encrypt = encrypt;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Date getPubdate() {
        return pubdate;
    }

    public void setPubdate(Date pubdate) {
        this.pubdate = pubdate;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public Long getTeacherId() {
        return teacherId;
    }

    public void setTeacherId(Long teacherId) {
        this.teacherId = teacherId;
    }

    public String getTeacherName() {
        return teacherName;
    }

    public void setTeacherName(String teacherName) {
        this.teacherName = teacherName;
    }

    public Long getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(Long categoryId) {
        this.categoryId = categoryId;
    }

    public String getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    public Long getCourseId() {
        return courseId;
    }

    public void setCourseId(Long courseId) {
        this.courseId = courseId;
    }

    public Boolean getHasReview() {
        return hasReview;
    }

    public void setHasReview(Boolean hasReview) {
        this.hasReview = hasReview;
    }

    public String getTeacherAvator() {
        return teacherAvator;
    }

    public void setTeacherAvator(String teacherAvator) {
        this.teacherAvator = teacherAvator;
    }

    public String getPaymentMethodName() {
        return paymentMethodName;
    }

    public void setPaymentMethodName(String paymentMethodName) {
        this.paymentMethodName = paymentMethodName;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    public PaymentMethod getPaymentMethod() {
        return paymentMethod;
    }

    public void setPaymentMethod(PaymentMethod paymentMethod) {
        this.paymentMethod = paymentMethod;
    }

    @Transient
    public String getCreateDateF(){
        return DateUtils.date2Str(super.getCreateDate());
    }

}